package br.kanban.core.busines;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.TypedQuery;

import br.kanban.core.persistence.Historico;
import br.kanban.core.persistence.Tarefa;
import br.kanban.core.persistence.Usuario;

@Stateless
public class HistoricoBO extends GenericBO<Historico> implements Serializable{

	private static final long serialVersionUID = 1L;

	@Override
	public List<Historico> pesquisar(Historico filtro) {
		StringBuilder sql =new StringBuilder("from Historico where 1=1 ");
		
		sql.append(filtro.getCodigo() != null ? "and codigo = :codigo " : "");
		
		TypedQuery<Historico> qr = getEm().createQuery(sql.toString(), Historico.class);
		
		if(filtro.getCodigo() != null)
			qr.setParameter("codigo", filtro.getCodigo());
		
		return qr.getResultList();
	}

	public Usuario findUsuarioFromTarefa(Tarefa tarefa){
		TypedQuery<Usuario> qr = getEm().createQuery("select h.usuario from Historico h where h.tarefa = :tarefa", Usuario.class);
		qr.setParameter("tarefa", tarefa);
		
		return qr.getResultList().get(0);
		
	}
	
}
